package com.hexacta.hrs.test.data;

import static com.hexacta.hrs.test.data.DomainTestDataDependencyCreator.HEXACTA_DOMAIN;
import static com.hexacta.hrs.test.data.LocationTestDataDependencyCreator.BUENOS_AIRES_CITY_LOCATION;
import static com.hexacta.hrs.test.data.PersonTestDataDependencyCreator.ADMIN_PERSON;
import static com.hexacta.hrs.test.data.SectorTestDataDependencyCreator.IT_SECTOR;

import java.util.List;
import java.util.Map;

import org.vectrics.company.Company;
import org.vectrics.company.Sector;
import org.vectrics.domain.Domain;
import org.vectrics.location.Address;
import org.vectrics.location.Location;
import org.vectrics.user.Person;

/**
 * Test data dependency creator for the Company class.
 * 
 * @author tpollak
 */
public class CompanyTestDataDependencyCreator extends AbstractTestDataDependencyCreator {

    public static final String HEXACTA_COMPANY = "HEXACTA_COMPANY";

    public CompanyTestDataDependencyCreator(List<TestDataDependencyCreator> dependencies) {
        super(dependencies);
    }

    public void createDependentTestData(Map<String, Object> context) {
        // Create one company for Hexacta
        Domain hexactaDomain = this.loadObjectFromContext(context, HEXACTA_DOMAIN);
        Company hexactaCompany = this.createHexactaCompany(context, hexactaDomain);
        hexactaCompany = this.insertDomainObject(hexactaCompany);

        // Update domain primary company
        hexactaDomain.setPrimaryCompany(hexactaCompany);
        this.insertDomainObject(hexactaDomain);

        this.storeObjectReferenceInContext(context, HEXACTA_COMPANY, hexactaCompany);
    }

    private Address createAddress(Map<String, Object> context) {
        Location buenosAiresCity = this.loadObjectFromContext(context, BUENOS_AIRES_CITY_LOCATION);
        Person admin = this.loadObjectFromContext(context, ADMIN_PERSON);
        Address address = new Address();
        address.setAddress1(this.getDataUtils().createAddressString());
        address.setAddress2(this.getDataUtils().createAddressString());
        address.setPostalCode(this.getDataUtils().createPostalCode());
        address.setLocation(buenosAiresCity);
        address.setCreatedBy(admin);
        address.setCreatedDate(this.getDataUtils().createCurrentDate());
        address.setLastModifiedBy(admin);
        address.setLastModifiedDate(this.getDataUtils().createCurrentDate());
        return address;
    }

    private Company createHexactaCompany(Map<String, Object> context, Domain hexactaDomain) {
        Person admin = this.loadObjectFromContext(context, ADMIN_PERSON);
        Sector itSector = this.loadCodedObjectFromContext(context, IT_SECTOR);
        Company hexactaCompany = new Company();
        hexactaCompany.setDomain(hexactaDomain);
        hexactaCompany.setSector(itSector);
        hexactaCompany.setCreatedBy(admin);
        hexactaCompany.setCreatedDate(this.getDataUtils().createCurrentDate());
        hexactaCompany.setLastModifiedBy(admin);
        hexactaCompany.setLastModifiedDate(this.getDataUtils().createCurrentDate());
        hexactaCompany.setName("Hexacta S.A.");
        hexactaCompany.setReferenceId("hx");
        hexactaCompany.setParent(null);
        hexactaCompany.setAddress(this.createAddress(context));
        hexactaCompany.setPhone(this.getDataUtils().createPhone());
        hexactaCompany.setPhone2(this.getDataUtils().createPhone());
        return hexactaCompany;
    }
}
